//
//  MenuTypeRN.swift
//  xdjcy
//
//  Created by Henry on 15/12/8.
//  Copyright (c) 2015年 Henry. All rights reserved.
//

import Foundation


class MenuTypeRN {
    
    var manager = AFHTTPRequestOperationManager()
    
    init()
    {
        var sp =  AFSecurityPolicy.defaultPolicy()
        sp.allowInvalidCertificates = true
        sp.validatesDomainName = false
        manager.securityPolicy = sp
    }
    
    class var sharedInstance:MenuTypeRN
    {
        struct Static {
            static let instance : MenuTypeRN = MenuTypeRN()
        }
        
        return Static.instance
    }
    
    
    /**
    获取所有类别
    */
    func getAllTypes(did:(result:Bool,message:String,jwJosn:Array<TypeModel>,flJson:Array<TypeModel>,ajJson:Array<TypeModel>)->Void)
    {
        manager.requestSerializer.timeoutInterval = RequestNetTimeoutInterval
        manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/html") as Set<NSObject>
        manager.POST(AllTypesMsgUrl, parameters: nil, success: { (operation, res) -> Void in
            var json = JSON(res)
            var obj = json["obj"]
            var jwobj = json["obj","001"].array
            var flobj = json["obj","002"].array
            var ajobj = json["obj","003"].array
            
            var jwModels = Array<TypeModel>()
            var flModels = Array<TypeModel>()
            var ajModels = Array<TypeModel>()
            
            if jwobj != nil
            {
                for jw in jwobj!
                {
                    jwModels.append(TypeModel(typecode: jw["typecode"].string, typename: jw["typename"].string))
                }
            }
            
            if flobj != nil
            {
                for fl in flobj!
                {
                    flModels.append(TypeModel(typecode: fl["typecode"].string, typename: fl["typename"].string))
                }
            }
            if ajobj != nil
            {
                for aj in ajobj!
                {
                    ajModels.append(TypeModel(typecode: aj["typecode"].string, typename: aj["typename"].string))
                }
            }
            
            did(result:json["success"].bool!, message:json["msg"].string!,jwJosn: jwModels, flJson: flModels, ajJson: ajModels)
            
            
            }) { (operation, error) -> Void in
                did(result:false, message:error.localizedDescription, jwJosn: Array<TypeModel>(), flJson: Array<TypeModel>(), ajJson: Array<TypeModel>())
        }
        
    }
    
    /**
    访问网络,获取底部菜单栏子分类
    */
    
    func getChildTypeList(type:String,did:(result:Bool,message:String,jsonResult:Array<TypeModel>)->Void)
    {
        manager.requestSerializer.timeoutInterval = RequestNetTimeoutInterval
        manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/html") as Set<NSObject>
        
        var params=["type":type]
        
        manager.POST(ArticleTypesMsgUrl, parameters: params, success: { (operation, res) -> Void in
            
            var json = JSON(res)
            var obj = json["obj"].array
            var models = Array<TypeModel>()
            if obj != nil
            {
                for item in obj!
                {
                    models.append(TypeModel(typecode: item["typecode"].string, typename: item["typename"].string))
                }
                
            }
            did(result: json["success"].bool!, message: json["msg"].string!, jsonResult: models)
            
            }) { (operation, error) -> Void in
                println(error)
                did(result: false, message: error.localizedDescription, jsonResult: Array<TypeModel>())
        }
        
        
    }
    
    func getArticlList(type:String,did:(result:Bool,message:String,imageResult:Array<ArticleModel>,articleResult:Array<ArticleModel>)->Void)
    {
        manager.requestSerializer.timeoutInterval = RequestNetTimeoutInterval
        manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/html") as Set<NSObject>
        
        var params=["type":type]
        
        manager.POST(ArticleListMsgUrl, parameters: params, success: { (operation, res) -> Void in
            
            var json = JSON(res)
            var picarticleobj = json["obj","picarticle"].array
            var articleobj = json["obj","article"].array
            var picarticleModels = Array<ArticleModel>()
            var articleModels = Array<ArticleModel>()
            if picarticleobj != nil
            {
                for picarticle in picarticleobj!
                {
                    picarticleModels.append(ArticleModel(id: picarticle["id"].string, title: picarticle["title"].string, imageName: picarticle["imageName"].string, imageHref: picarticle["imageHref"].string, summary: picarticle["summary"].string, content: picarticle["content"].string, author: picarticle["author"].string, source: picarticle["source"].string, columnId: picarticle["columnId"].string, istop: picarticle["istop"].string, isdisplay: picarticle["isdisplay"].string))
                }
                
            }
            if articleobj != nil
            {
                for article in articleobj!
                {
                    articleModels.append(ArticleModel(id: article["id"].string, title: article["title"].string, imageName: article["imageName"].string, imageHref: article["imageHref"].string, summary: article["summary"].string, content: article["content"].string, author: article["author"].string, source: article["source"].string, columnId: article["columnId"].string, istop: article["istop"].string, isdisplay: article["isdisplay"].string))
                }
            }
            
            did(result: json["success"].bool!, message: json["msg"].string!, imageResult: picarticleModels, articleResult: articleModels)
            
            }) { (operation, error) -> Void in
                did(result: false, message: error.localizedDescription, imageResult: Array<ArticleModel>(), articleResult: Array<ArticleModel>())
        }
        
    }
    
    func getArticlePageList(type:String,page:String,did:(result:Bool,message:String,jsonResult:Array<ArticleModel>)->Void)
    {
        manager.requestSerializer.timeoutInterval = RequestNetTimeoutInterval
        manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/html") as Set<NSObject>
        
        var params=["type":type,"page":page]
        
        manager.POST(ArticlePageListMsgUrl, parameters: params, success: { (operation, res) -> Void in
            
            var json = JSON(res)
            var obj = json["obj"].array
            var models = Array<ArticleModel>()
            if obj != nil
            {
                for item in obj!
                {
                    models.append(ArticleModel(id: item["id"].string, title:item["title"].string, imageName: item["imageName"].string, imageHref: item["imageHref"].string, summary: item["summary"].string, content: item["content"].string, author: item["author"].string, source: item["source"].string, columnId: item["columnId"].string, istop: item["istop"].string, isdisplay: item["isdisplay"].string))
                }
            }
            did(result: json["success"].bool!, message: json["msg"].string!, jsonResult: models)
            
            }) { (operation, error) -> Void in
                did(result: false, message: error.localizedDescription, jsonResult: Array<ArticleModel>())
        }
    }
    
    func getBasicList(did:(result:Bool,message:String,cardList:Array<TypeModel>,jobLevelList:Array<TypeModel>,identityList:Array<TypeModel>,crimesList:Array<TypeModel>)->Void)
    {
        manager.requestSerializer.timeoutInterval = RequestNetTimeoutInterval
        manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/html") as Set<NSObject>
        manager.POST(BasicListMsgUrl, parameters: nil, success: { (operation, res) -> Void in
            var json = JSON(res)
            var cardObj = json["obj","idcardtyp"].array
            var jobObj = json["obj","joblevel"].array
            var identityObj = json["obj","identity"].array
            var crimesObj = json["obj","crimes"].array
            
            var cardModels = Array<TypeModel>()
            var jobModels = Array<TypeModel>()
            var identityModels = Array<TypeModel>()
            var crimesModels = Array<TypeModel>()
            
            if cardObj != nil
            {
                for card in cardObj!
                {
                    cardModels.append(TypeModel(typecode: card["typecode"].string, typename: card["typename"].string))
                }
            }
            if jobObj != nil
            {
                for job in jobObj!
                {
                    jobModels.append(TypeModel(typecode: job["typecode"].string, typename: job["typename"].string))
                }
            }
            if identityObj != nil
            {
                for identity in identityObj!
                {
                    identityModels.append(TypeModel(typecode: identity["typecode"].string, typename: identity["typename"].string))
                }
            }
            if crimesObj != nil
            {
                for crimes in crimesObj!
                {
                    crimesModels.append(TypeModel(typecode: crimes["typecode"].string, typename: crimes["typename"].string))
                }
            }
            
            did(result: json["success"].bool!, message: json["msg"].string!, cardList: cardModels, jobLevelList: jobModels, identityList: identityModels, crimesList: crimesModels)
            
            }) { (operation, error) -> Void in
                did(result: false, message: error.localizedDescription, cardList: Array<TypeModel>(), jobLevelList: Array<TypeModel>(), identityList: Array<TypeModel>(), crimesList: Array<TypeModel>())
        }
    }
    
    func ReportSearchResult(report_idcardtype:String,report_idcard:String,did:(result:Bool,message:String,jsonResult:ReportModel)->Void)
    {
        manager.requestSerializer.timeoutInterval = RequestNetTimeoutInterval
        manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/html") as Set<NSObject>
        var params = ["report_idcardtype":report_idcardtype,"report_idcard":report_idcard]
        manager.POST(ReportSearchMsgUrl, parameters: params, success: { (operation, res) -> Void in
            var json = JSON(res)
            var model = ReportModel()
            var obj = json["obj"]
            if obj["id"] != nil
            {
                model.Id =  json["obj","id"].intValue
                model.Resolve_status = json["obj","resolve_status"].string
                model.Resolve_content = json["obj","resolve_content"].string
                model.Reporttrail_id = json["obj","reporttrail_id"].intValue
            }
            did(result: json["success"].bool!, message: json["msg"].string!, jsonResult: model)
            
            }) { (operation, error) -> Void in
                did(result: false, message: error.localizedDescription, jsonResult: ReportModel())
        }
        
    }
    
    func ReportSave(jbmodel:JBModel,did:(result:Bool,message:String)->Void)
    {
        manager.requestSerializer.timeoutInterval = RequestNetTimeoutInterval
        manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/html") as Set<NSObject>
        var params = ["report_name":jbmodel.Report_name!,"report_phone":jbmodel.Report_phone!,"report_idcardtype":jbmodel.Report_idcardtype!,"report_idcard":jbmodel.Report_idcard!,"reported_name":jbmodel.Reported_name!,"reported_joblevel":jbmodel.Reported_joblevel!,"reported_company":jbmodel.Reported_company!,"reported_cimearea":jbmodel.Reported_cimearea!,"reported_idendtity":jbmodel.Reported_idendtity!,"reported_allegedcrimes":jbmodel.Reported_allegedcrimes!,"reported_content":jbmodel.Reported_content!]
        
        manager.POST(ReportSaveMsgUrl, parameters: params, success: { (operation, res) -> Void in
            
            var json = JSON(res)
            var flag = json["success"].bool!
            if flag
            {
                did(result: true, message: json["msg"].string!)
            }
            else
            {
                did(result: false, message: json["msg"].string!)
            }
            
        }) { (operation, error) -> Void in
            did(result: false, message: error.localizedDescription)
        }
    }
    
    
    func SearchList(title:String,did:(result:Bool,message:String,jsonResult:Array<ArticleModel>)->Void)
    {
        manager.requestSerializer.timeoutInterval = RequestNetTimeoutInterval
        manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/html") as Set<NSObject>
        var params = ["title":title]
        manager.POST(SearchMsgUrl, parameters: params, success: { (operation, res) -> Void in
            
            var json = JSON(res)
            var obj = json["obj"].array
            var models = Array<ArticleModel>()
            if obj != nil
            {
                for item in obj!
                {
                    models.append(ArticleModel(id: item["id"].string, title: item["title"].string, imageName: item["imageName"].string, imageHref: item["imageHref"].string, summary: item["summary"].string, content: item["content"].string, author: item["author"].string, source: item["source"].string, columnId: item["columnId"].string, istop: item["istop"].string, isdisplay: item["isdisplay"].string))
                }
            }
            did(result: json["success"].bool!, message: json["msg"].string!, jsonResult: models)
            
        }) { (operation, error) -> Void in
            did(result: false, message: error.localizedDescription, jsonResult: Array<ArticleModel>())
        }
    }
}